<?php

/**
 * object 转 array
 */
function object_to_array($obj){
    $_arr = is_object($obj) ? get_object_vars($obj) : $obj;
    foreach ($_arr as $key => $val) {
        $val = (is_array($val)) || is_object($val) ? object_to_array($val) : $val;
        $arr[$key] = $val;
    }
    return $arr;
}


//遍历分类
function resolveTree($items,$parent_id = NULL){
        foreach ($items as $key => $value) {
        $select = "";
        if($value['id'] == $parent_id )
            $select = "selected";
        echo "<option  value=".$value['id']." ".$select." >".$value['category_name']."</option>";
            if(isset($value['son'])){
                foreach($value['son'] as $kk=>$vv){
                    $spt = "|--";
                    
                    $vv['num'] = 1;
                    $list[0] = $vv;
                    while(!empty($list)){
                       $sptt = "";
                        $son = array_shift($list);

                        for($i = 0;$i<$son['num'];$i++)
                            $sptt .= "&nbsp;&nbsp;&nbsp;";

                    $select = "";
                    if($son['id'] == $parent_id )
                        $select = "selected";
                    echo "<option  value=".$son['id']."  ".$select." >".$sptt.$spt.$son['category_name']."</option>";

                    if(isset($son['son']) && !empty($son['son'])){
                        $num = ++$son['num'];
                        for ($i = count($son['son'])-1;$i>=0;$i--) {
                            $son['son'][$i]['num'] = $num;
                            array_unshift($list,$son['son'][$i]);
                        }
                        
                    }
                }
                }
            }
       
        
       }
}

//通过引用生成无限分类
// items 要生成的数组
function generateTree($items){
    $tree = array();
    
    foreach($items as $key=>$item){
       if($item['parent_id'] === 0){
            $tree[] = &$items[$key];
            continue;
       }
       foreach ($items as $k => $v) {

           if($v['id'] === $item['parent_id'])
                $items[$k]['son'][] = &$items[$key];
       }
    }
    return $tree;
}

/**
 * 发送验证码
 */
function send_yanzhengma($phone,$param){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, 'https://open.ucpaas.com/ol/sms/sendsms');
    curl_setopt($curl, CURLOPT_POST, 1);

 
    $post_data = array(
         "sid" => "8cf1d18ed6e07c3571533079ce569012",
         "token" => "77949679e58c07dac24093ac391e8b39",
         "appid" => "d7cd102958a44762a6107d4fcbec411f",
         "templateid" => "450778",
         "param" => $param,
         "mobile" => $phone,
         );
    $post_data = json_encode($post_data);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($post_data)
    ));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
     curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
     //执行命令
    $data = curl_exec($curl);
     //关闭URL请求
    if(curl_errno($curl)) {
     print curl_error($curl);
    }
    curl_close($curl);
     //显示获得的数据
     var_dump($data);
       
}
/**
 * 获取客户端IP地址
 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
 * @param boolean $adv 是否进行高级模式获取（有可能被伪装） 
 * @return mixed
 */
function get_client_ip($type = 0,$adv=false) {
    $type       =  $type ? 1 : 0;
    static $ip  =   NULL;
    if ($ip !== NULL) return $ip[$type];
    if($adv){//高级模式获取(防止伪装)
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos    =   array_search('unknown',$arr);
            if(false !== $pos) unset($arr[$pos]);
            $ip     =   trim($arr[0]);
        }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip     =   $_SERVER['HTTP_CLIENT_IP'];
        }elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip     =   $_SERVER['REMOTE_ADDR'];
        }
    }elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ip     =   $_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法验证
    $long = sprintf("%u",ip2long($ip));
    $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
    return $ip[$type];
    }
?>